package com.cargo.base.utils;

import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class JxlsUtil {
	
	public static void doJxlsExl(String tempPath,String outputPath,Map<String,Object> beans){
		
		InputStream in = null;
		
		OutputStream out = null;
		
		try {
			
			in = new BufferedInputStream(new FileInputStream(tempPath));
			
			out = new FileOutputStream(outputPath);
			
			//生成JXLS操作对象
			XLSTransformer transformer = new XLSTransformer();
			
			Workbook workbook = transformer.transformXLS(in, beans);
			
			workbook.write(out);
			
			out.flush();
			
			System.out.println("========>>文件导出成功<<======");
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (in!=null){try {in.close();} catch (IOException e) {}}
            if (out!=null){try {out.close();} catch (IOException e) {}}
		}
	}
	
	public static void main(String[] args) {
		try {
			
			String tempPath = "D:\\testFile\\testJxls.xlsx";
			
			String outputPath = "D:\\testFile\\testJxls"+System.currentTimeMillis()+".xlsx";
			
			//模拟数据
			Map<String,Object> beans = new HashMap<String,Object>();
			
			beans.put("test", JxlsTestBean.build("t1XXX", "t2XXX", "t3XXXONSSCCSSFFDDEEFF\nRRRRRRRRRRRRRRRRRRRRRRRRRRR"));
			
			doJxlsExl(tempPath, outputPath ,beans);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
